package com.annhoa.system.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.annhoa.core.base.BasePage;
import com.annhoa.core.base.DAO;
import com.annhoa.system.domain.User;
import com.annhoa.system.repository.UserJpaRepository;
import com.annhoa.system.repository.UserRepository;
import com.annhoa.util.PageData;

@Service
@Transactional
public class UserService {
	@Autowired
	private UserJpaRepository userJpaRepository;
	@Autowired
	private UserRepository userRepository;

	@Autowired
	private DAO dao;

	/*
	 * 查找所有
	 */
	public List<User> findAll() {
		return userJpaRepository.findAll();
	}

	/*
	 * 用户名查找
	 */
	public List<User> findByName(String name) {
		return userRepository.findByName(name);
	}

	/*
	 * springdata保存
	 */
	public void saveUser(User user) {
		userJpaRepository.save(user);
	}

	/*
	 * id 查
	 */
	@Cacheable("users")
	public Object findById(long id) {
		System.out.println("Cached Pages");
		return userJpaRepository.findById(id);
	}

	/*
	 * id 删
	 */
	public void delete(long id) {
		userJpaRepository.deleteById(id);
	}

	public User findUserByUsername(String username){
		return userRepository.findByUsername(username);
	}

	/**
	 * name 查 排序
	 * 
	 * @param name
	 * @param sort
	 * @return
	 */
	public List<User> findByName(String name, Sort sort) {
		return userRepository.findByName(name, sort);
	}

	/*
	 * 调用DaoSupport name 联表查询
	 */
	@SuppressWarnings("unchecked")
	public List<PageData> findForList(PageData pd) throws Exception {
		String sql = "select u.* ,a.name address from user u,address a where a.address_id = u.address_id and u.name=:name"; // 注意语法
		// return (List<PageData>) daoSupport.queryListEntity(sql, pd,null);
		return (List<PageData>) dao.findForList(sql, pd, null);
	}

	/*
	 * 调用DaoSupport id 查 return PageData
	 */
	public PageData findForObject(PageData pd) throws Exception {
		String sql = "select * from user where id=:id";
		return (PageData) dao.findForObject(sql, pd, null);
	}

	/*
	 * 调用DaoSupport id 查 return List<User>
	 */
	@SuppressWarnings("unchecked")
	public List<User> findForList(User user) throws Exception {
		String sql = "select * from user";
		return (List<User>) dao.findForList(sql, user, User.class);
	}

	/*
	 * @params pd 调用DaoSupport 保存
	 */
	public void save(PageData pd) throws Exception {
		String sql = "insert into user(id,username,password,name,address_id,age) values(:id,:username,:password,:name,:address_id,:age)";
		dao.save(sql, pd);
	}

	/**
	 * 分页、排序
	 * 
	 * @param page
	 * @return
	 * @throws Exception
	 */

	@SuppressWarnings({ "rawtypes" })
	public Page<Map> pageList(BasePage page) throws Exception {
		String sql = "select * from user order by age";
		page.setSql(sql);
		return dao.findPageByParams(page);
	}

}